﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace StrongPassword
{
    public partial class main : Form
    {
        //passwd
        public string basePasswd= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        public int capacity = 10;
        //sock 
        public IPAddress IP = IPAddress.Parse("31.0.158.109");
        private byte[] result = new byte[1024];
        public Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        public string GetPasswd(string basePasswd,int capacity) {
            string passwd="";
            Random random = new Random(Guid.NewGuid().GetHashCode());
            for (int i = 0; i < capacity; i++) {
                passwd += basePasswd[random.Next(0, basePasswd.Length)].ToString();
                    }
            return passwd ;
        }
        public void linkServer() {
            socketClient.Connect(new IPEndPoint(IP, 9999));
            int receiveLength = socketClient.Receive(result);
            if (Encoding.UTF8.GetString(result, 0, receiveLength) == "连接成功")
            {
                buttonSend.Enabled = true;
                buttonSend.ForeColor = Color.YellowGreen;
            }
            else
            {
                buttonSend.Enabled = false;
                buttonSend.ForeColor = Color.Red;
            };
        }

        public main()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBoxbasePasswd.Text = basePasswd;
            textBoxcapacity.Text = capacity.ToString();
            passwd0.Text = GetPasswd(basePasswd,capacity);
            passwd1.Text = GetPasswd(basePasswd, capacity);
            passwd2.Text = GetPasswd(basePasswd, capacity);
            passwd3.Text = GetPasswd(basePasswd, capacity);
            passwd4.Text = GetPasswd(basePasswd, capacity);
            passwd5.Text = GetPasswd(basePasswd, capacity);
            Clipboard.SetDataObject(passwd0.Text, true);

        }

        private void passwd_Click(object sender, EventArgs e)
        {
            Label lab = (Label)sender;
            passwd0.Text = lab.Text;
            Clipboard.SetDataObject(passwd0.Text, true);
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            capacity = int.Parse(textBoxcapacity.Text);
            basePasswd = textBoxbasePasswd.Text;
            if (textBoxbasePasswd.Text=="i am v.") {
                passwd0.Text = "我爱你，老婆。";
                passwd1.Text = passwd2.Text = passwd3.Text = passwd4.Text = passwd5.Text = "💗💗💗💗💗💗";
            }
            else { 
            passwd0.Text = GetPasswd(basePasswd, capacity);
            passwd1.Text = GetPasswd(basePasswd, capacity);
            passwd2.Text = GetPasswd(basePasswd, capacity);
            passwd3.Text = GetPasswd(basePasswd, capacity);
            passwd4.Text = GetPasswd(basePasswd, capacity);
            passwd5.Text = GetPasswd(basePasswd, capacity);
            }
            //剪切板
            Clipboard.SetDataObject(passwd0.Text, true);

        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            if (textBoxUserName.Text != "")
            {
                string sendMsg = textBoxUserName.Text + " " + passwd0.Text;
                socketClient.Send(Encoding.UTF8.GetBytes(sendMsg));
            };
        }

        private void checkBoxLink_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                linkServer();
            }
            catch
            {
                buttonSend.Enabled = false;
                buttonSend.ForeColor = Color.Red;
            }
        }
    }
}
